package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallCash;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallCashService;
import org.linlinjava.litemall.db.service.LitemallSystemConfigService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.util.CashConstant;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static org.linlinjava.litemall.wx.util.WxResponseCode.BALANCE_ALREADY;
import static org.linlinjava.litemall.wx.util.WxResponseCode.BALANCE_LESS;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/cash")
@Validated
public class WxCashController {
    private final Log logger = LogFactory.getLog(WxCashController.class);

    @Autowired
    private LitemallCashService cashService;

    @Autowired
    private LitemallUserService userService;

    @Autowired
    private LitemallSystemConfigService systemConfigService;

    private Object validate(LitemallCash cash) {
        return null;
    }

    @GetMapping("rule")
    public Object rule() {
        Map<String, String> rule = systemConfigService.listCash();
        return ResponseUtil.ok(rule);
    }

    /**
     * 提现记录
     */
    @RequestMapping("/list")
    public Object list(@LoginUser Integer userId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer size,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallCash> litemallCashes = cashService.querySelective(userId, null, page, size, sort, order);
        return ResponseUtil.okList(litemallCashes);
    }

    @PostMapping("submit")
    public Object submit(@LoginUser Integer userId, @RequestBody LitemallCash cash) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        Object error = validate(cash);
        if (error != null) {
            return error;
        }
        Map<String, String> rule = systemConfigService.listCash();
        if (cash.getAmount().compareTo(new BigDecimal(rule.get("litemall_cash_less_amount")))<0) {
            return ResponseUtil.fail(400, "提现金额不得小于系统规定最低金额");
        }
        LitemallCash unCheck = cashService.findByUidUnCheck(userId);
        if (unCheck != null) {
            return ResponseUtil.fail(BALANCE_ALREADY, "已有待审核申请，请耐心等待审核");
        }
        LitemallUser user = userService.findById(userId);
        String username = user.getNickname();
        if (cash.getAmount().compareTo(user.getBalance()) > 0) {
            return ResponseUtil.fail(BALANCE_LESS, "余额不足");
        }
        cash.setId(null);
        cash.setUserId(userId);
        cash.setUsername(username);
        cash.setMobile(user.getMobile());
        cash.setStatus(CashConstant.STATUS_WAIT);
        cashService.add(cash);

        return ResponseUtil.ok();
    }
}
